<?php
	
	include '../inc/db.php';
	$cn = new db_mysqli();

	$user_id = isset($_GET['id']) ? $_GET['id'] : 0;

	if ($user_id > 0) {

		//$user_id = $_GET['id'];
		$query = 'select bookings.id, bookings.start, bookings.end, bookings.spaceid, bookings.paid, spaces.label as title from bookings INNER JOIN spaces ON bookings.spaceid = spaces.id where userid='.$user_id;
		$data = $cn->Data($query);
		$arr = array();
		foreach($data as $row) {
			array_push($arr, array(
				'id'=>$row['id'],
				'title'=>$row['title'],//.' '.substr($row['start'], 11, 5).' - '.substr($row['end'], 11, 5),
				'start'=>$row['start'],
				'end'=>$row['end'],
				'url'=>'bookings/view/'.$row['id'],
				'className'=>($row['paid']==1 ? 'event-blue' : 'event-red')
			));
		}
		echo json_encode($arr);

	} else {

		$query = 'select bookings.id, bookings.userid, bookings.start, bookings.end, bookings.spaceid, bookings.paid, spaces.label as title from bookings INNER JOIN spaces ON bookings.spaceid = spaces.id';
		$data = $cn->Data($query, false);
		$arr = array();
		foreach($data as $row) {
			
			$user = $cn->Data('select name, company, iscompany from users where id='.$row['userid'], false);
			$user = $user[0];
			$name = $user['name'];
			if ($user['iscompany'] == 1) $name = $user['company'];

			array_push($arr, array(
				'id'=>$row['id'],
				'title'=>$name.': '.$row['title'],//.' '.substr($row['start'], 11, 5).' - '.substr($row['end'], 11, 5),
				'start'=>$row['start'],
				'end'=>$row['end'],
				'url'=>'bookings/view/'.$row['id'],
				'className'=>($row['paid']==1 ? 'event-blue' : 'event-red')
			));
		}
		echo json_encode($arr);

	}
?>